package com.cqw.controller;

import com.cqw.common.BaseResponse;
import com.cqw.domain.Activity;
import com.cqw.domain.Area;
import com.cqw.exception.CommonException;
import com.cqw.mapper.one.AreaMapper;
import com.cqw.service.ActivityService;
import com.cqw.validate.Add;
import com.cqw.validate.Update;
import com.cqw.vo.ActivityVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-05-13 10:38
 * @Description: 活动api
 */
@RestController
@RequestMapping("/activity")
@Slf4j
@Api(tags = "活动api")
public class ActivityController {


    @Autowired
    private ActivityService activityService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    public BaseResponse activityList(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        List<Activity> list = activityService.getActivityList(pageIndex, pageSize);
        return BaseResponse.success(list);
    }


    @PostMapping("/create")
    @ApiOperation(value = "新建活动")
    public BaseResponse createActivity(@RequestBody @Validated(Add.class) ActivityVO activityVO) {
        long now = System.currentTimeMillis();
        checkActivity(activityVO, now);
        Activity activity = activityService.createActivity(activityVO);
        return BaseResponse.success(activity);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改活动")
    public BaseResponse updateActivity(@RequestBody @Validated(Update.class) ActivityVO activityVO) {
        long now = System.currentTimeMillis();
        checkActivity(activityVO, now);
        activityService.updateActivity(activityVO);
        return BaseResponse.success();
    }


    @GetMapping("/info")
    @ApiOperation(value = "活动详情")
    public BaseResponse activityInfo(@RequestParam("activityId") Long activityId) {
        Activity activityInfo = activityService.getActivityInfo(activityId, false);
        return BaseResponse.success(activityInfo);
    }


    @GetMapping("/publish")
    @ApiOperation(value = "发布活动")
    public BaseResponse publishActivity(@RequestParam("activityId") Long activityId) {
        activityService.publishActivity(activityId);
        return BaseResponse.success();
    }


    private void checkActivity(ActivityVO activityVO, long now) {
        if (activityVO.getStartTime().getTime() < now) {
            throw new CommonException("活动开始时间不能小于当前时间");
        }

        if (activityVO.getStartTime().getTime() >= activityVO.getEndTime().getTime()) {
            throw new CommonException("活动结束时间不能小于等于活动开始时间");
        }
    }


    @Autowired
    private AreaMapper areaMapper;


    /**
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/test/page")
    public String testPage(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                           @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        int offset;
        if (pageIndex <= 1) {
            //判断有没有自己的然后再考虑是否减掉
            offset = 0;
            pageSize = 14;
        } else {
            offset = 14 + (pageIndex - 2) * pageSize;
        }

        Area areas1 = new Area();
        PageHelper.offsetPage(offset, pageSize, true);
        List<Area> areas = areaMapper.selectAll();
        areas.add(0,areas1);
        if (areas instanceof Page) {
            Page page = (Page) areas;

            long total = page.getTotal();

            System.out.println(total);
        }
        return "OK";
    }

}
